package rsj.dao;

import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.util.StringUtils;
import rsj.db.dbutils.UniqueID;
import rsj.util.PageQuery;
import rsj.util.PaginationData;

import java.sql.Connection;
import java.sql.Timestamp;

public class OperateAuDao extends BaseDao {
    private static final OperateAuDao instance = new OperateAuDao();

    public static OperateAuDao getInstance() {
        return instance;
    }

    private static final String tableName = "sys.sys_authon";
    private static final String allColumns = "aut_id,aut_keyid,aut_signkey,aut_secretkey,aut_keyversion,aut_ctime,aut_endtime,aut_org_id";

    public static void insert(String keyId, String signKey, String secretKey
            , String keyVersion, Timestamp createTime, Timestamp outTime, Connection conn, String orgId) {
        String sql = String.format(insertSql, tableName, allColumns, "?,?,?,?,?,?,?,?");
        getQueryRunner().update(conn, sql, UniqueID.generateId(), keyId, signKey, secretKey, keyVersion, createTime, outTime, orgId);
    }

    public Node getByOrgId(String orgId, Connection conn) {
        String sql = String.format(select, " count(1) ", tableName, " aut_org_id = ? ");
        return getQueryRunner().queryLinkNode(conn, sql, orgId);
    }

    public PaginationData<TableNode> getAuthInfoList(PageQuery pageQuery, String searchName) {
        PaginationData<TableNode> paginationData = new PaginationData<TableNode>();
        String likeSearch = "";
        String sqlCount = String.format(selectWithoutWhere, "count(1)", tableName );
        // 2020年11月6日16:51:52 添加模糊搜索需求
        if (!StringUtils.isEmpty(searchName)) {
            likeSearch = " WHERE org.org_sname LIKE '%" + searchName + "%'";
            sqlCount = "SELECT count(1) FROM sys.sys_authon authon "
                    + " LEFT JOIN sys.sys_org org ON authon.aut_org_id = org.org_id"
                    + " LEFT JOIN sys.sys_user use ON authon.aut_org_id = use.user_org_id" + likeSearch;
        }
        String finalLikeSearch = likeSearch;
        String finalSqlCount = sqlCount;
        return (PaginationData<TableNode>) invokeSql(conn -> {
            String sqlList = "SELECT authon.*,org.org_name,org.org_sname,use.user_uname FROM sys.sys_authon authon "
                    + " LEFT JOIN sys.sys_org org ON authon.aut_org_id = org.org_id"
                    + " LEFT JOIN sys.sys_user use ON authon.aut_org_id = use.user_org_id"
                    + finalLikeSearch
                    + " order by aut_ctime desc  "
                    + " limit " + pageQuery.getLimit()
                    + " offset " + pageQuery.getOffset();

            Node node = getQueryRunner().queryLinkNode(conn, finalSqlCount);
            TableNode tableNode = getQueryRunner().queryLinkTableNode(conn, sqlList);

            paginationData.setTotalCount(node.valueInt());
            paginationData.setDataList(tableNode);

            return paginationData;
        });
    }
}
